package cn.iocoder.yudao.module.book.controller.admin.members;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.members.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.members.ClassMembersDO;
import cn.iocoder.yudao.module.book.service.members.ClassMembersService;

@Tag(name = "管理后台 - 班级成员")
@RestController
@RequestMapping("/book/class-members")
@Validated
public class ClassMembersController {

    @Resource
    private ClassMembersService classMembersService;

    @PostMapping("/create")
    @Operation(summary = "创建班级成员")
    @PreAuthorize("@ss.hasPermission('book:class-members:create')")
    public CommonResult<Integer> createClassMembers(@Valid @RequestBody ClassMembersSaveReqVO createReqVO) {
        return success(classMembersService.createClassMembers(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级成员")
    @PreAuthorize("@ss.hasPermission('book:class-members:update')")
    public CommonResult<Boolean> updateClassMembers(@Valid @RequestBody ClassMembersSaveReqVO updateReqVO) {
        classMembersService.updateClassMembers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级成员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:class-members:delete')")
    public CommonResult<Boolean> deleteClassMembers(@RequestParam("id") Integer id) {
        classMembersService.deleteClassMembers(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级成员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:class-members:query')")
    public CommonResult<ClassMembersRespVO> getClassMembers(@RequestParam("id") Integer id) {
        ClassMembersDO classMembers = classMembersService.getClassMembers(id);
        return success(BeanUtils.toBean(classMembers, ClassMembersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级成员分页")
    @PreAuthorize("@ss.hasPermission('book:class-members:query')")
    public CommonResult<PageResult<ClassMembersRespVO>> getClassMembersPage(@Valid ClassMembersPageReqVO pageReqVO) {
        PageResult<ClassMembersDO> pageResult = classMembersService.getClassMembersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassMembersRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级成员 Excel")
    @PreAuthorize("@ss.hasPermission('book:class-members:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassMembersExcel(@Valid ClassMembersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassMembersDO> list = classMembersService.getClassMembersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级成员.xls", "数据", ClassMembersRespVO.class,
                        BeanUtils.toBean(list, ClassMembersRespVO.class));
    }

}